package se.teamscape.server.rpc;

import java.util.List;

import net.customware.gwt.dispatch.server.ActionHandler;
import net.customware.gwt.dispatch.server.ExecutionContext;
import net.customware.gwt.dispatch.shared.ActionException;
import se.teamscape.server.db.Datastore;
import se.teamscape.shared.model.Model;
import se.teamscape.shared.rpc.ModelsQuery;
import se.teamscape.shared.rpc.ModelsQueryResult;

import com.google.inject.Inject;

/**
 * Handler for retrieving models from datastore using a custom query.
 * 
 * @author Andreas Borglin
 * 
 */
public class ModelsQueryHandler implements ActionHandler<ModelsQuery, ModelsQueryResult> {

    private final Datastore mDatastore;

    @Inject
    public ModelsQueryHandler(Datastore datastore) {
        this.mDatastore = datastore;
    }

    @Override
    public ModelsQueryResult execute(ModelsQuery arg0, ExecutionContext arg1) throws ActionException {

        List<Model> models = mDatastore.getModelsByQuery(arg0.getQuery());

        return new ModelsQueryResult(models);
    }

    @Override
    public Class<ModelsQuery> getActionType() {
        return ModelsQuery.class;
    }

    @Override
    public void rollback(ModelsQuery arg0, ModelsQueryResult arg1, ExecutionContext arg2) throws ActionException {
        // TODO Auto-generated method stub

    }

}
